﻿using System;
using System.Collections;
using System.Data;

public partial class Admin_News_QL_BanTinPL : System.Web.UI.Page
{
    Data data = new Data();
    int indexOfImg = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["admin"].Equals("true") == false)
            {
                Response.Redirect("../Default.aspx");
            }
            else
            {
                FillData();
            }
        }
        catch (Exception)
        {
            Response.Redirect("../Default.aspx");
            throw;
        }
    }
    private void FillData()
    {
        Calendar1.SelectedDate = DateTime.Now;
        if (Request.QueryString["delete"] != null)
        {
            data.Delete_BanTinPL(Request.QueryString["delete"]);
        }
        DataTable table = data.GetTable_By_Name("BanTinPhapLuat");
        DataListYahoo.DataSource = table;
        DataListYahoo.DataBind();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (checkTextbox())
        {
            String fileanh = "";
            Random random = new Random();
            String noidung = CKEditor1.Text;

            if (FileUpload1.HasFile)
            {
                int _random = random.Next(100000);
                fileanh = FileUpload1.FileName.Split('.')[0] + "_" + _random + "." + FileUpload1.FileName.Split('.')[1] + ";";
                FileUpload1.SaveAs(Server.MapPath("~/Images/AnhBanTinPhapLuat/" + FileUpload1.FileName.Split('.')[0] + "_" + _random + "." + FileUpload1.FileName.Split('.')[1]));
                if (chk_source.Checked == false)
                {
                    noidung = ReplaceImgSrc(noidung, FileUpload1.FileName.Split('.')[0] + "_" + _random, indexOfImg);
                }
            }
            if (FileUpload2.HasFile)
            {
                int _random = random.Next(100000);
                fileanh = fileanh + FileUpload2.FileName.Split('.')[0] + "_" + _random + "." + FileUpload2.FileName.Split('.')[1];
                FileUpload2.SaveAs(Server.MapPath("~/Images/AnhBanTinPhapLuat/" + FileUpload2.FileName.Split('.')[0] + "_" + _random + "." + FileUpload2.FileName.Split('.')[1]));
                if (chk_source.Checked == false)
                {
                    noidung = ReplaceImgSrc(noidung, FileUpload2.FileName.Split('.')[0] + "_" + _random, indexOfImg);
                }
            }
            if (FileUpload3.HasFile)
            {
                int _random = random.Next(100000);
                fileanh = fileanh + FileUpload3.FileName.Split('.')[0] + "_" + _random + "." + FileUpload3.FileName.Split('.')[1] + ";";
                FileUpload3.SaveAs(Server.MapPath("~/Images/AnhBanTinPhapLuat/" + FileUpload3.FileName.Split('.')[0] + "_" + _random + "." + FileUpload3.FileName.Split('.')[1]));
                if (chk_source.Checked == false)
                {
                    noidung = ReplaceImgSrc(noidung, FileUpload3.FileName.Split('.')[0] + "_" + _random, indexOfImg);
                }
            }
            if (FileUpload4.HasFile)
            {
                int _random = random.Next(100000);
                fileanh = fileanh + FileUpload4.FileName.Split('.')[0] + "_" + _random + "." + FileUpload4.FileName.Split('.')[1] + ";";
                FileUpload4.SaveAs(Server.MapPath("~/Images/AnhBanTinPhapLuat/" + FileUpload4.FileName.Split('.')[0] + "_" + _random + "." + FileUpload4.FileName.Split('.')[1]));
                if (chk_source.Checked == false)
                {
                    noidung = ReplaceImgSrc(noidung, FileUpload4.FileName.Split('.')[0] + "_" + _random, indexOfImg);
                }
            }
            if (FileUpload5.HasFile)
            {
                int _random = random.Next(100000);
                fileanh = fileanh + FileUpload5.FileName.Split('.')[0] + "_" + _random + "." + FileUpload5.FileName.Split('.')[1] + ";";
                FileUpload5.SaveAs(Server.MapPath("~/Images/AnhBanTinPhapLuat/" + FileUpload5.FileName.Split('.')[0] + "_" + _random + "." + FileUpload5.FileName.Split('.')[1]));
                if (chk_source.Checked == false)
                {
                    noidung = ReplaceImgSrc(noidung, FileUpload5.FileName.Split('.')[0] + "_" + _random, indexOfImg);
                }
            }
            data.Add_BanTinPL(Calendar1.SelectedDate.ToShortDateString(), noidung, txtTieude.Text, txtNDTomtat.Text, fileanh);
            Response.Redirect("Default.aspx");
        }
    }
    protected void BtnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
    protected String ReplaceImgSrc(String noidung, String noidungInsert, int _indexOfImg)
    {
        ArrayList imgSrc = new ArrayList();
        int indexOfSrc = noidung.IndexOf("src=", _indexOfImg);
        if (indexOfSrc != -1)
        {
            int indexOfGachcheo = noidung.IndexOf("/>", indexOfSrc);
            int indexofDot = noidung.IndexOf(".", indexOfSrc);
            if (indexofDot < indexOfGachcheo && indexOfGachcheo != -1 && indexofDot != -1)
            {
                int khoangcach = indexofDot - indexOfSrc - 5;
                noidung = noidung.Remove(indexOfSrc + 5, khoangcach);
                noidung = noidung.Insert(indexOfSrc + 5, "../Images/AnhBanTinPhapLuat/" + noidungInsert);
            }
            indexOfImg = indexOfGachcheo;
        }

        return noidung;
    }
    private bool checkTextbox()
    {
        if (txtTieude.Text.Trim().Length == 0)
        {
            lblError.Text = "Tiêu đề không được trống";
            lblError.Visible = true;
            return false;
        }
        if (txtNDTomtat.Text.Trim().Length == 0)
        {
            lblError.Text = "Nội Dung tóm tắt không được trống";
            lblError.Visible = true;
            return false;
        }
        if (CKEditor1.Text.Trim().Length == 0)
        {
            lblError.Text = "Nội dung không được trống";
            lblError.Visible = true;
            return false;
        }
        if (txtNDTomtat.Text.Length > 200)
        {
            lblError.Text = "Nội dung tóm tắt phải < 200 ký tự";
            lblError.Visible = true;
            return false;
        }
        if (txtTieude.Text.Length > 200)
        {
            lblError.Text = "Tiêu đề phải < 200 ký tự";
            lblError.Visible = true;
            return false;
        }
        return true;
    }
}
